//
//  RankSonListViewController.m
//  GSG
//
//  Created by gaox on 15/4/25.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "RankSonListViewController.h"
#import "AFNetworking.h"
#import "MusicModel.h"
#import "UIImageView+WebCache.h"
#import "SongBookCell.h"
#import "PlayerViewController.h"
#import "SongModel.h"
#import "MBProgressHUD.h"
#import"singleSong.h"
#import"PlayListViewController.h"
@interface RankSonListViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)NSMutableArray *modelArr;
@property (nonatomic,retain)UITableView *rankTableView;
@property(nonatomic,retain)MBProgressHUD*HUD;
@end

@implementation RankSonListViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        self.modelArr = [NSMutableArray array];
    }
    
    return self;
}



- (void)data{

       NSString *str = [NSString stringWithFormat:@"http://api.dongting.com/channel/ranklist/%ld/songs?page=1&app=ttpod&v=v7.7.0.2015012818&uid=&mid=iPhone4&f=f320&s=s310&imsi=&hid=&splus=7.1.1&active=1&net=2&openudid=ad6f50f586818c4f36ff4358053c1b53fee58cdf&idfa=E2EAC64B-1F1C-4AC5-A0E1-8BE37A03FB96&utdid=VTCyBQSkYzwDAALO9zZTFfPC&alf=201200",self.musicModel.id];
//    NSLog(@" -------%@",str);

    

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSDictionary *dict = responseObject;
        
        NSMutableArray *arr = [dict objectForKey:@"data"];

        for (NSInteger i = 0; i < arr.count ; i++) {
            SongModel *mo = [[SongModel alloc]init];
            NSMutableDictionary *dic1 = arr[i];
            mo.song_name=[dic1 objectForKey:@"name"];
            mo.singer_name=[dic1 objectForKey:@"singerName"];
            NSMutableArray *arr = [dic1 objectForKey:@"urlList"];
            

            for (NSDictionary *dic  in arr) {
                mo.url=[dic objectForKey:@"url"];
                
            }
           
            [self.modelArr addObject:mo];
            [mo release];

//            MusicModel *movie = [[MusicModel alloc] init];
//            NSMutableDictionary *movieDic = arr[i];
//            [movie setValuesForKeysWithDictionary:movieDic];
//            [self.modelArr addObject:movie];
//            [movie release];

        }

        [self.rankTableView reloadData];
        [_HUD hide:YES];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"No");
        NSLog(@"%@",error);
    }];
}
-(void)actionBuLand{
    PlayListViewController *plvc=[[PlayListViewController alloc]init];
    [self.navigationController pushViewController:plvc animated:YES];
    [plvc release];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"播放" style:UIBarButtonItemStyleDone target:self action:@selector(actionBuLand)];
    self.navigationItem.rightBarButtonItem.tintColor=[UIColor whiteColor];
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bujing.png"]];
    [self data];
    self.title = self.musicModel.title;
    
    
    self.rankTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-self.view.frame.size.height * 60 / 667) style:UITableViewStylePlain];
    self.rankTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.rankTableView];
    self.rankTableView.rowHeight = 100;
    self.rankTableView.dataSource = self;
    self.rankTableView.delegate = self;
    //小菊花
    //方式1.直接在View上show
    _HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];
    
    
    //常用的设置
    //小矩形的背景色
    _HUD.color = [UIColor clearColor];//这儿表示无背景
    //显示的文字
    _HUD.labelText = @"提示";
    //细节文字
    _HUD.detailsLabelText = @"正在加载内容";
    //是否有庶罩
    _HUD.dimBackground = YES;

    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return  self.modelArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reuse = @"reuse";
    SongBookCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[SongBookCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse] autorelease];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    SongModel *song=self.modelArr[indexPath.row];
    NSString *str = [NSString stringWithFormat:@"%ld.%@",indexPath.row +1,song.song_name];
    
    cell.backgroundColor = [UIColor clearColor];
    cell.song_nameLabel.text = str;
    cell.singer_nameLabel.text = song.singer_name;
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    

    [singleSong shareSingleSong].arrSong=self.modelArr;
    [singleSong shareSingleSong].num=indexPath.row;
    
    NSNotificationCenter *center=[NSNotificationCenter  defaultCenter];
    
    [center postNotificationName:@"播放" object:@"收到了看见了" userInfo:nil];
    
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
